﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using GA_Scheduler;
using Acceron.University.DBAccess;


namespace Acceron.Univerisy.Modules.Scheduling
{
    /// <summary>
    /// Interaction logic for DayScheduleControl.xaml
    /// </summary>
    public partial class DayScheduleControl : UserControl
    {
        List<Group> _groups;
        int _nDayOfWeek = 0;

        public int DayOfTheWeek
        {
            get { return _nDayOfWeek; }
            set
            {
                _nDayOfWeek = value;
                switch (_nDayOfWeek)
                {
                    case 1:
                        label1.Content = "Понедельник";
                        break;
                    case 2:
                        label1.Content = "Вторник";
                        break;
                    case 3:
                        label1.Content = "Среда";
                        break;
                    case 4:
                        label1.Content = "Четверг";
                        break;
                    case 5:
                        label1.Content = "Тяпница";
                        break;
                    case 6:
                        label1.Content = "Суббота";
                        break;
                    case 7:
                        label1.Content = "Воскресение";
                        break;
                    default:
                        label1.Content = "НЕОПРЕДЕЛёННЫЙ ДЕНЬ";
                        break;
                }
            }
        }

        public List<Group> Groups
        {
            get { return _groups; }
            set
            {
                _groups = value;

                schGrid.ColumnDefinitions.Clear();
                int i = 0;
                foreach (var gr in _groups)
                {
                    schGrid.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = new GridLength(100, GridUnitType.Star),
                            Tag = gr.Group_ID
                        });

                    Label nLblb = new Label()
                    {
                        Content = gr.Name
                    };
                    nLblb.SetValue(Grid.ColumnProperty, i);
                    schGrid.Children.Add(nLblb);
                    i++;
                }
            }
        }

        public DayScheduleControl()
        {
            InitializeComponent();
        }

        public void Clear()
        {
            schGrid.Children.Clear();

            int i = 0;
            foreach (var gr in _groups)
            {
                Label nLblb = new Label()
                {
                    Content = gr.Name
                };
                nLblb.SetValue(Grid.ColumnProperty, i);
                schGrid.Children.Add(nLblb);
                i++;
            }
        }

        //public void AddNewCourse(int time, int grId, string txt)
        public void AddNewCourse(Gen schedule)
        {
            if (schedule.Class.Course == null)
                return;

            Brush[] brushes = new Brush[] { Brushes.Aqua, Brushes.Gold, Brushes.GreenYellow};

            int columnDisplay = 0;
            int columnSpan = 1;
            int rowDisplay = schedule.Pair;
            int rowSpan = schedule.Class.Duration;
            int groupCount = schedule.Class.GroupClasses.Count;
            string txt = schedule.Class.Course.Name + "\n" + schedule.Class.Professor.Name + "\n" + schedule.Room.Name;

            ColumnDefinition lastColumn = null;

            /*
            Delegate show = new Action(delegate
                {
                    Border nBord = new Border()
                    {
                        //Height = this.schGrid.RowDefinitions[time + 1].ActualHeight,
                        //Width = this.schGrid.ColumnDefinitions.First( c => (int)c.Tag == _class.GroupClasses).ActualWidth, //;/[grId - 1].ActualWidth,
                        BorderThickness = new Thickness(2),
                        BorderBrush = Brushes.Black,
                        Background = brushes[new Random().Next() % brushes.Count()]
                    };

                    nBord.SetValue(Grid.ColumnSpanProperty, schedule.Class.Duration);
                    nBord.SetValue(Grid.RowProperty, schedule.Pair + 1);
                    nBord.SetValue(Grid.ColumnProperty, rowDisplay - 1);
                    nBord.SetValue(Grid.RowSpanProperty, rowSpan);

                    nBord.Child = new Label()
                    {
                        Content = txt
                    };

                    schGrid.Children.Add(nBord);
                }
                );
            */


            int lastIndex = -1;
            var sortedList = schedule.Class.GroupClasses.OrderBy(g => g.Group.Name).ToList();
            for (int i = 0; i < groupCount; i++)
            {
                var gr = sortedList[i];
                var curColumnDef = this.schGrid.ColumnDefinitions.First(c => (int)c.Tag == gr.Groupd_ID);
                int curColDispl = this.schGrid.ColumnDefinitions.IndexOf(curColumnDef);


                if (lastIndex != -1)
                {
                    if (lastIndex + 1 != curColDispl)
                    {
                        //show.DynamicInvoke();
                        Border nBord = new Border()
                        {
                            BorderThickness = new Thickness(2),
                            BorderBrush = Brushes.Black,
                            Background = brushes[new Random().Next() % brushes.Count()],
                            CornerRadius = new CornerRadius(20)
                        };

                        nBord.SetValue(Grid.ColumnSpanProperty, columnSpan);
                        nBord.SetValue(Grid.ColumnProperty, columnDisplay);
                        nBord.SetValue(Grid.RowProperty, rowDisplay + 1);
                        nBord.SetValue(Grid.RowSpanProperty, rowSpan);

                        nBord.Child = new Label()
                        {
                            Content = txt
                        };

                        columnDisplay = curColDispl;
                        schGrid.Children.Add(nBord);
                        columnSpan = 1;
                        //lastColumn = null;
                    }
                    else
                    {
                        columnSpan++;
                    }
                }
                else
                {
                    columnDisplay = curColDispl;
                }
                lastIndex = curColDispl;
                
            }

            Border nBord2 = new Border()
            {
                BorderThickness = new Thickness(2),
                BorderBrush = Brushes.Black,
                CornerRadius = new CornerRadius(20),
                Background = brushes[new Random().Next() % brushes.Count()]
            };
            
            //DayClassControl nBord2 = new DayClassControl(schedule);

            nBord2.SetValue(Grid.ColumnSpanProperty, columnSpan);
            nBord2.SetValue(Grid.RowProperty, rowDisplay + 1);
            nBord2.SetValue(Grid.ColumnProperty, columnDisplay);
            nBord2.SetValue(Grid.RowSpanProperty, rowSpan);

            nBord2.Child = new Label()
            {
                Content = txt,
                Margin = new Thickness(10, 0, 0, 0)
            };

            schGrid.Children.Add(nBord2);
            //show.DynamicInvoke();

            
        }

        private void PaintCourse(int column, int grId, string txt)
        {

        }
    }   
}
